package com.bs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.entity.Acceptability;
import com.bs.entity.ClockInfo;
import com.bs.entity.EmployeeInfo;
import com.bs.service.AcceptabilityService;
import com.bs.service.ClockInfoService;
import com.bs.util.FaceVideo;
import com.bs.util.Window;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import java.util.*;

@RestController
@RequestMapping("/ClockInfo")
public class ClockInfoCon {
    @Autowired
    ClockInfoService clockInfoService;

    @Autowired
    EmployeeInfoCon employeeInfoCon;

    @Autowired
    AcceptabilityService acceptabilityService;
    private FaceVideo faceVideo = new FaceVideo();

    @GetMapping("/selectById")
    public ClockInfo selectById(Integer id){
        return selectById(id);
    }

    @GetMapping("/selectALL")
    public List<ClockInfo> selectAll(){
        return clockInfoService.list();
    }


    // 新增和修改打卡信息
    @PostMapping("/saveClockInfo")
    public Boolean saveClockInfo(ClockInfo clockInfo){
        return clockInfoService.saveClockInfo(clockInfo);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean deleteClockInfo(@PathVariable Integer id){
        return clockInfoService.removeById(id);
    }

    @GetMapping("/page")
    public IPage<ClockInfo> findPage(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , @RequestParam(required = false) String name
            , @RequestParam(required = false) Integer id){
        IPage<ClockInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<ClockInfo> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(name)){
            queryWrapper.lambda().like(ClockInfo::getName,name);
        }
        if(Objects.nonNull(id)){
            queryWrapper.lambda().eq(ClockInfo::getId,id);
        }
        return clockInfoService.page(page, queryWrapper);
    }

    //和本地存储的图片进行对比
    @GetMapping("/compareFace")
    @Transactional(rollbackFor = Exception.class)
    public boolean compareFace(Integer id){
        Integer sys = clockInfoService.getClock();
        // 查询结果为0 代表管理后台未开启打卡功能
        if(sys == 0){
            return false;
        }
        Window win = new Window();
        Thread thread = new Thread(win);
        thread.start();
        while (!win.isSetFlag()) {
            try {
                Thread.sleep(1000);
            }catch (Exception e){
                System.out.println(e);
            }

        }
        boolean flag = win.isFlag();
        if (flag){
            EmployeeInfo employeeInfo = employeeInfoCon.selectById(id);
            ClockInfo clockInfo = new ClockInfo();
            // 将员工能够的属性转换为打卡信息的属性

            BeanUtils.copyProperties(employeeInfo,clockInfo);
            clockInfoService.saveClockInfo(clockInfo);

            //需要判断考勤合格纪律中有没有对应同员工号码，同年，同月的
            //判断是否存在打卡记录，如果是新员工则添加新数据
            //获取年月信息
            //Calendar calendar = Calendar.getInstance();
            //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //String format = formatter.format(calendar.getTime());
            //String year = format.substring(0,4);
            //String month = format.substring(5,7);
            QueryWrapper<Acceptability> acceptabilityQueryWrapper = new QueryWrapper<>();
            acceptabilityQueryWrapper.lambda().eq(Acceptability::getId,id);

            if(Objects.isNull(acceptabilityService.list(acceptabilityQueryWrapper))){
                acceptabilityService.addAcceptability(id);
            }else {
                acceptabilityService.updateAcceptability(id);
            }
        }
        return flag;
    }

//修改能否打卡权限
    @GetMapping("/changClockSys")
    public void changClockSys(@RequestParam Boolean flag){
        clockInfoService.changClockSys(flag);
    }

//修改能否修改密码权限
    @GetMapping("/changePasswordSys")
    public void changePasswordSys(@RequestParam Boolean passwordFlag){
        clockInfoService.changePasswordSys(passwordFlag);
    }
//修改请假时长限制
    @GetMapping("/changeDuration")
    public boolean changeDuration(@RequestParam Integer duration){
//        System.out.println(duration);
        //最长为30天
        if (duration > 30){
            return false;
        }
        return clockInfoService.changeDuration(duration);
    }
//获取初始权限信息
    @GetMapping("/getSys")
    public HashMap<String, Integer> getSys(){
        //打卡权限
        Integer flag = clockInfoService.getClock();
        //修改密码权限
        Integer passwordFlag = clockInfoService.getPwd();
        //请假时长
        Integer duration = clockInfoService.getDuration();
//        ArrayList<Integer> integers = new ArrayList<>();
        HashMap<String, Integer> sysMap = new HashMap<>();
        sysMap.put("flag",flag);
        sysMap.put("passwordFlag",passwordFlag);
        sysMap.put("duration",duration);
        return sysMap;
    }
}
